package com.hit.sys.config;

import cn.hutool.core.collection.CollectionUtil;
import com.hit.core.consts.CommonConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.List;

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    private List<Parameter> getParameters() {
        Parameter parameter = new ParameterBuilder()
                .name("Authorization")
                .description("token令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();

        List<Parameter> parameters = CollectionUtil.newArrayList();
        parameters.add(parameter);
        return parameters;
    }

    @Bean
    public Docket defaultApi() {
        List<Parameter> parameters = getParameters();
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(defaultApiInfo())
                .groupName("默认接口")
                .select()
                .apis(RequestHandlerSelectors.basePackage(CommonConstant.DEFAULT_PACKAGE_NAME))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters);
    }

    private ApiInfo defaultApiInfo() {
        return new ApiInfoBuilder()
                .title("Thor Doc")
                .description("Thor Doc文档")
                .termsOfServiceUrl("https://com.hit")
                .contact(new Contact("hit", "https://com.hit", "fanhailei_hit@163.com"))
                .version("1.0")
                .build();
    }

//    @Bean
//    public Docket groupRestApi() {
//        List<Parameter> parameters = getParameters();
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(defaultApiInfo())
//                .groupName("2-登录管理接口")
//                .select()
//                //TODO 这里改为自己的包名
//                .apis(RequestHandlerSelectors.basePackage("com.hit.sys.modular.auth"))
//                .paths(PathSelectors.any())
//                .build()
//                .globalOperationParameters(parameters);
//    }

//    private ApiInfo groupApiInfo() {
//        return new ApiInfoBuilder()
//                .title("Thor Doc")
//                .description("Thor Doc文档")
//                .termsOfServiceUrl("https://com.hit")
//                .contact(new Contact("hit", "https://com.hit", "fanhailei_hit@163.com"))
//                .version("1.0")
//                .build();
//    }
}
